package com.example.zselasticsearchsf.controller;

import com.alibaba.fastjson.JSON;
import com.example.zselasticsearchsf.common.CommonResult;
import com.example.zselasticsearchsf.config.SearchUtil;
import com.example.zselasticsearchsf.domain.CaseInfoBean;
import com.example.zselasticsearchsf.domain.PcontextInfoBean;
import com.example.zselasticsearchsf.dto.SfwDto;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.rest.RestStatus;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p> 中国司法网案例库爬虫</p>
 *
 * @Author: Steven
 * @Date: 2021/10/20 7:48
 */
@RestController
@RequestMapping("zs/reptile")
@Slf4j
public class Reptile {


    @Autowired
    @Qualifier(value = "restHighLevelClient")
    private RestHighLevelClient client;

    @Resource
    private SearchUtil searchUtil;

    private static String TARGET_URL = "http://alk.12348.gov.cn/LawMultiSearch?searchField=%E6%A1%88%E4%BE%8B%E5%85%A8%E6%96%87&keywords=";//全文检索

    @GetMapping("/sfw")
    public void htmlUnitSFW(@RequestParam(value = "keywords", required = false) String keywords,
                            @RequestParam(value = "pages", required = false) Integer pages) throws IOException, InterruptedException {

        if (keywords == null) {//null不可搜索
            keywords = "";
        }

        SfwDto sfwDto = new SfwDto();
        CaseInfoBean caseInfoBean=new CaseInfoBean();
        PcontextInfoBean pcontextInfoBean=new PcontextInfoBean();

        final WebClient webClient = new WebClient(BrowserVersion.CHROME);//新建一个模拟谷歌Chrome浏览器的浏览器客户端对象

        webClient.getOptions().setThrowExceptionOnScriptError(false);//当JS执行出错的时候是否抛出异常, 这里选择不需要
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);//当HTTP的状态非200时是否抛出异常, 这里选择不需要
        webClient.getOptions().setActiveXNative(false);
        webClient.getOptions().setCssEnabled(false);//是否启用CSS, 因为不需要展现页面, 所以不需要启用
        webClient.getOptions().setJavaScriptEnabled(true); //很重要，启用JS
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());//很重要，设置支持AJAX


        HtmlPage page = null;
            page = webClient.getPage(TARGET_URL);//尝试加载上面图片例子给出的网页

            //模拟点击检索
            HtmlInput loginId = page.getHtmlElementById("keywords");
            loginId.setValueAttribute(keywords);//关键词
            Thread.sleep(5000);//todo !!! 不睡眠加载不出

            page.getHtmlElementById("submitButton").click();
            Thread.sleep(5000);//todo !!!不睡眠加载不出

            DomElement searchResultDiv = (DomElement) (page.getHtmlElementById("searchResultDiv").getLastElementChild());

            System.out.println("-----------------------1--------------------------------");
            System.out.println(searchResultDiv);
            System.out.println("-----------------------2--------------------------------");
            System.out.println(page.getHtmlElementById("searchResultDiv").getByXPath("//div[@class='pagenav']/a"));
            List<HtmlAnchor> htmlAnchorList = page.getHtmlElementById("searchResultDiv").getByXPath("//div[@class='pagenav']/a");
            System.out.println(htmlAnchorList.get(htmlAnchorList.size() - 2));
            Document doc = Jsoup.parse(htmlAnchorList.get(htmlAnchorList.size() - 2).asXml());
            Integer totalNum = Integer.valueOf(doc.getElementsByTag("a").text());


            log.info(">>>>>>>>>>>>>>搜索关键词:【{}】，搜索结果：【{}】页", keywords, totalNum);
            for (int j=1;j<=totalNum;j++){
                String JavaScrip="searchPageNew('"+j+"')";
                page.executeJavaScript(JavaScrip);
                Thread.sleep(5000);//todo !!!不睡眠加载不出
                log.info("第>>>>>>>>>>>>>>>【{}】页", j);



        webClient.waitForBackgroundJavaScript(30000);//异步JS执行需要耗时,所以这里线程要阻塞30秒,等待异步JS执行结束

        String pageXml = page.asXml();//直接将加载完成的页面转换成xml格式的字符串
        //TODO 下面的代码就是对字符串的操作了,常规的爬虫操作,用到了比较好用的Jsoup库

        Document document = Jsoup.parse(pageXml);//获取html文档
        System.out.println("==========================");
//        System.out.println(document);
        List<Element> infoListEle = document.getElementById("searchResultDiv").getElementsByTag("tbody").first().getElementsByTag("tbody").first().getElementsByTag("tr");//获取元素节点等
        List<Element> infoListEle2 = document.getElementById("searchResultDiv").getElementsByAttributeValue("class", "pagenav");//获取元素节点等


        infoListEle2.forEach(element -> {
            System.out.println(element.getElementsByTag("a").text());
            System.out.println(element.getElementsByTag("a").attr("href"));
        });

        infoListEle.forEach(element -> {

            List<Element> infoListEle1 = element.getElementsByTag("td");//获取元素节点等

            for (int i = 0; i <= infoListEle1.size() - 1; i++) {
                caseInfoBean.setCaseId((i+1)+"");
                caseInfoBean.setApprover("Approver");
                caseInfoBean.setAnnex("annex");
                caseInfoBean.setPublishHair("publishHai");
                caseInfoBean.setTextType("setTextType");
                caseInfoBean.setAuthor("老蔡的蔡");
                caseInfoBean.setAnnex1("setAnnex1");
                caseInfoBean.setApproveStart("setApproveStart");
                caseInfoBean.setIsPublish(i%2==0? "是":"否");
                caseInfoBean.setKeyword("我是关键字，我很棒");
                caseInfoBean.setUploadDeptId("我是员工id");
                caseInfoBean.setIsTopping(i%2+"");
                caseInfoBean.setSybiosis("~~~简介");
                caseInfoBean.setThumbnail("缩略图字段");

                System.out.println(infoListEle1.get(i).getElementsByTag("td").first().text());
                if (i == 0) {
                    System.out.println("http://alk.12348.gov.cn" + infoListEle1.get(i).getElementsByTag("td").first().getElementsByTag("a").attr("href"));
                    sfwDto.setCaseTittlDateUtilse(infoListEle1.get(i).getElementsByTag("td").first().text());
                    caseInfoBean.setCaseTitle(infoListEle1.get(i).getElementsByTag("td").first().text());

//                    sfwDto.setCaseUrl("http://alk.12348.gov.cn" + infoListEle1.get(i).getElementsByTag("td").first().getElementsByTag("a").attr("href"));
                    sfwDto.setCaseUrl(infoListEle1.get(i).getElementsByTag("td").first().getElementsByTag("a").attr("href"));
//                    caseInfoBean.setContext(("http://alk.12348.gov.cn" + infoListEle1.get(i).getElementsByTag("td").first().getElementsByTag("a").attr("href")));
                    // TODO: 2021/10/21 继续跳转页面，爬取文章 内容
                    String newUrl="http://alk.12348.gov.cn" + infoListEle1.get(i).getElementsByTag("td").first().getElementsByTag("a").attr("href");
//                   String id
                    try {
                    /* 此方法获取  Document 会一直在这一页循环
                    HtmlPage pageDetail  = webClient.getPage(newUrl);//尝试加载上面图片例子给出的网页
                        String DetailPageXml= pageDetail.asXml();//直接将加载完成的页面转换成xml格式的字符串 下面的代码就是对字符串的操作了,常规的爬虫操作,用到了比较好用的Jsoup库
                        Document DetailDocument = Jsoup.parse(DetailPageXml);//获取html文档*/
                       Document DetailDocument = Jsoup.parse(new URL(newUrl), 30000);

                        List<Element> infoListEle2w= DetailDocument.getElementsByAttributeValue("class","wzinfo");//获取元素节点等
                        System.out.println("详情页==================");
                        System.out.println( infoListEle2w.get(0).html());
                        caseInfoBean.setContext(infoListEle2w.get(0).html());

                    } catch (IOException e) {
                        e.printStackTrace();
                    }


                } else {
                    if(i == 1){
                        sfwDto.setNumber(infoListEle1.get(i).getElementsByTag("td").first().text());
                        caseInfoBean.setColumnId(infoListEle1.get(i).getElementsByTag("td").first().text());
                    }else if(i == 2){
                        sfwDto.setSubmissionTime(infoListEle1.get(i).getElementsByTag("td").first().text().substring(0,10));
                        caseInfoBean.setApproveDate(infoListEle1.get(i).getElementsByTag("td").first().text().substring(0,10));
                        caseInfoBean.setPublishDate(infoListEle1.get(i).getElementsByTag("td").first().text().substring(0,10));
                        caseInfoBean.setUploadDate(infoListEle1.get(i).getElementsByTag("td").first().text().substring(0,10));
                    }else if(i == 3){
                        sfwDto.setCaseType(infoListEle1.get(i).getElementsByTag("td").first().text());
                        caseInfoBean.setSource(infoListEle1.get(i).getElementsByTag("td").first().text());
                    }else if(i == 4) {
                        sfwDto.setVisits(Integer.valueOf(infoListEle1.get(i).getElementsByTag("td").first().text()));
                        caseInfoBean.setColumnId(infoListEle1.get(i).getElementsByTag("td").first().text());
                    }

                }
            }
//            createDocByUtils(caseInfoBean);


            pcontextInfoBean.setContextId(caseInfoBean.getCaseId());
            pcontextInfoBean.setContextTitle(caseInfoBean.getCaseTitle());
            pcontextInfoBean.setContextSubtitle(caseInfoBean.getCaseTitle());
            pcontextInfoBean.setColumnId(caseInfoBean.getColumnId());
            pcontextInfoBean.setAuthor("风靡的糜");
            pcontextInfoBean.setAuditor("山峰的山");
            pcontextInfoBean.setAuditStart("2");
            pcontextInfoBean.setAuditIdea("没有意见");
            pcontextInfoBean.setAuditDate(caseInfoBean.getApproveDate());
            pcontextInfoBean.setIsPublish("1");
            pcontextInfoBean.setPublishHair("小王");
            pcontextInfoBean.setPublishDate(caseInfoBean.getApproveDate());
            pcontextInfoBean.setSource(caseInfoBean.getSource());
            pcontextInfoBean.setSybiosis(caseInfoBean.getSybiosis());

            pcontextInfoBean.setContext(caseInfoBean.getContext());
            pcontextInfoBean.setUploadDeptId(caseInfoBean.getUploadDeptId());
            pcontextInfoBean.setIsTopping(caseInfoBean.getIsTopping());
            pcontextInfoBean.setAttachment("我是附件 我是附件我是附件我是附件");
            pcontextInfoBean.setColName("colName");

            createDocByUtils3(pcontextInfoBean);
            Map<String ,String> map=new HashMap<>();
//            map.put("content",caseInfoBean.getContext());
//            createDocByUtils2(map);
/*            try {
                CreateESDocument(sfwDto);
            } catch (IOException e) {
                System.out.println("报错继续下一条");
                e.printStackTrace();
            }*/
        });

            }
    }


    public void CreateESDocument(SfwDto sfwDto ) throws IOException {

        //创建请求
        IndexRequest request =new IndexRequest("sfw");
        //规则   put /kuang_index/_doc/1
//        request.id( "1");//不设置自动
        request.timeout ( TimeValue.timeValueSeconds( 1));
        request.timeout ( "3s" );
        //  将我们的数据放入请求json
        request.source(JSON.toJSONString(sfwDto), XContentType.JSON);
//        客户端发送请求,获取响应的结果

        IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
        System.out.println(indexResponse.toString());//返回这行记录（doc）
        System.out.println(indexResponse.status());;//对应我们命令返回的状态CREATED

    }

    public void createDocByUtils(CaseInfoBean caseInfoBean){
        //eg 创建对象 插件去
        RestStatus res = null;
        try {
            res=  searchUtil.createDocument(caseInfoBean,"case_info");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void createDocByUtils2(Map<String,String> content){
        //eg 创建对象 插件去
        RestStatus res = null;
        try {
            res=  searchUtil.createDocument(content,"full_content");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void createDocByUtils3(PcontextInfoBean pcontextInfoBean){
        //eg 创建对象 插件去
        RestStatus res = null;
        try {
            res=  searchUtil.createDocument(pcontextInfoBean,"law_info");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
